/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * window.java
 *
 * Created on Apr 30, 2010, 11:25:29 PM
 */

package server;

import client.*;
import java.awt.event.KeyEvent;

/**
 *
 * @author buyandalai
 */
public class chatHandlerWindow extends javax.swing.JFrame {
    chatHandler ch;
    /** Creates new form window */
    public chatHandlerWindow(chatHandler ch) {
        this.ch = ch;
        initComponents();
        this.connected();
    }



    public void connected()
    {
        label_remote_ip.setText("Client IP: "+ch.getRemoteIpAddress());
        history.setEnabled(true);
        write.setEnabled(true);
        send.setEnabled(true);
    }

    public void disconnected()
    {
        history.setEnabled(false);
        write.setEnabled(false);
        send.setEnabled(false);
    }

    private void sendMessage()
    {
        String msg = this.write.getText().trim();
        if(msg.length() > 0)
        {
            ch.sendMessage(msg);
            this.addHistory("Me >> "+msg);
            this.write.setText("");
        }
    }




    public void clientMessage(String msg)
    {
        this.addHistory(ch.getRemoteIpAddress()+" >> "+msg);
    }

    private void addHistory(String str)
    {
        history.setText(str+"\n"+history.getText());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        label_remote_ip = new javax.swing.JLabel();
        label_message = new javax.swing.JLabel();
        send = new javax.swing.JButton();
        message_history = new javax.swing.JScrollPane();
        history = new javax.swing.JTextArea();
        message_write = new javax.swing.JScrollPane();
        write = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        label_remote_ip.setText("Client IP:");

        label_message.setText("Message:");

        send.setText("Send");
        send.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendActionPerformed(evt);
            }
        });

        history.setColumns(20);
        history.setEditable(false);
        history.setRows(5);
        message_history.setViewportView(history);

        write.setColumns(20);
        write.setRows(5);
        write.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                write_keypressed(evt);
            }
        });
        message_write.setViewportView(write);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(message_history, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(message_write, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)
                    .addComponent(send, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_message, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label_remote_ip, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_remote_ip)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(message_history, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(label_message)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(message_write, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(send)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void sendActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        this.sendMessage();
    }

    private void write_keypressed(java.awt.event.KeyEvent evt) {
        // TODO add your handling code here:
        if(evt.getKeyCode() == KeyEvent.VK_ENTER)
        {
            this.sendMessage();
        }
    }

    private void formWindowClosed(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        //ch.closeSocket();
    }



    // Variables declaration - do not modify
    private javax.swing.JTextArea history;
    private javax.swing.JLabel label_message;
    private javax.swing.JLabel label_remote_ip;
    private javax.swing.JScrollPane message_history;
    private javax.swing.JScrollPane message_write;
    private javax.swing.JButton send;
    private javax.swing.JTextArea write;
    // End of variables declaration

}
